#ifndef QQUICK_RO_DATA_H
#define QQUICK_RO_DATA_H

#include <QObject>
#include <QVariantMap>
class ROHost;
class RONode;
class ROComponent;
class QQmlComponent;
typedef quint32 ObjectId;

static const ObjectId NullId = 0;

//用于记录一个对象的网络共享信息
struct ObjectData
{
    ROHost* host = nullptr;
    RONode* node = nullptr;
    ObjectId id;
    QObject* obj;
};
typedef std::shared_ptr<ObjectData> ObjectDataPtr;
typedef std::weak_ptr<ObjectData> ObjectDataWPtr;

typedef QMap<ObjectId,QList<ObjectId>> CreationInfo;//一个对象创建完毕后的树状形态

//用于记录一个对象的创建信息
struct GenerateData
{
    ObjectId root;//根对象id
    CreationInfo creationinfos;
    QStringList reason;//创建理由
};
typedef std::shared_ptr<GenerateData> GenerateDataPtr;

//用于保证指针类数据能够在网络中正常传递
struct ROId
{
    ROId();
    ROId(const ROId& other);
    ROId(quint32 id);
    quint32 id = 0;
};

Q_DECLARE_METATYPE(ROId)

void setROObjectData(QObject* obj,ObjectDataPtr data);
ObjectDataPtr getROObjectData(QObject* obj);
void clearROObjectData(QObject* obj);

typedef std::function<ObjectId(QObject*)> ToObjectId;
typedef std::function<QObject*(ObjectId)> FromObjectId;

//指针数据想要发送到网络，必须要转换成ObjectId，到了另一端再转换回来
void convertValueForNet(QVariant & value,const ToObjectId& );
void convertValueFromNet(QVariant& value,const FromObjectId&);

template<typename T>
class IdGenerator
{
public:
    IdGenerator(T t = 0):id(t){

    }

    T alloc(){
        id++;
        return id;
    }

private:
    T id;
};

void invokeMethod(QObject* obj,QString name,const QVariantList& params);


QObject *createObject(QQmlComponent* comp,QObject *parent, const QVariantMap &properties);

#endif
